package com.gougou.manager.web.controller.api;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gougou.common.entity.EasyUIResult;
import com.gougou.common.entity.ItemResult;
import com.gougou.manager.pojo.TbItem;
import com.gougou.manager.pojo.TbItemDesc;
import com.gougou.manager.service.MPTbItemDescService;
import com.gougou.manager.service.MPTbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/manager/itemApi")
public class itemApi {
    @Autowired
    private MPTbItemService tbItemService;
    @Autowired
    private MPTbItemDescService tbItemDescService;
    /**
     * 根据id查询商品集合
     * @param id
     * @return
     */
    @RequestMapping("/itemListbyId")
    public List getItemListById(@RequestParam Long id){
        try {
            EasyUIResult easyUIResult = new EasyUIResult();

            EntityWrapper<TbItem> itemWrapper = new EntityWrapper<TbItem>();
            itemWrapper.where("id = {0}",id);
            itemWrapper.orderBy("created desc");//按照创建时间倒序
            //查询数据
            return tbItemService.selectList(itemWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 根据id查询商品信息
     * @param id
     * @return
     */
    @RequestMapping("/item/{id}")
    public ItemResult getItemById(@PathVariable Long id){

        TbItem tbItem = tbItemService.selectById(id);
        //查询封装基本信息
        ItemResult itemResult =new ItemResult();
        itemResult.setId(tbItem.getId());
        itemResult.setTitle(tbItem.getTitle());
        itemResult.setSellPoint(tbItem.getSellPoint());
        itemResult.setPrice(tbItem.getPrice());
        itemResult.setImage(tbItem.getImage());

        //查询详细信息
        EntityWrapper<TbItemDesc> tbItemDescEntityWrapper =new EntityWrapper<TbItemDesc>();
        tbItemDescEntityWrapper.where("item_id={0}",id);
        TbItemDesc itemDesc = tbItemDescService.selectOne(tbItemDescEntityWrapper);
        if (itemDesc==null){
            itemResult.setItemDesc(null);
            return itemResult;
        }

        itemResult.setItemDesc(itemDesc.getItemDesc());
        return itemResult;
    }

}
